var needle = require('needle');
var redisManager =require('../proxy/redisManager');
var config = require('../config').config;

//TODO: 重构入WeiXinApi模块中
exports.getWechatAccToken = function(callback){
    var url = 'https://api.weixin.qq.com/cgi-bin/token?' +
        'grant_type=client_credential&appid='+config.wechat_appid+'&secret='+config.wechat_secret;
    var option = {headers: {'Content-Type': 'application/json'}}
    needle.get(url, option, function(err, resp, body){
        if(err) return callback(err);
        var data = body;//JSON.parse(body);
        redisManager.saveAccesstoken(data.access_token, function(err, data){
            console.log('Refresh access_token :'+ data)
            callback(err, body);
        })
    })
}
